package com.globalresources.commons;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
/**
 * 
 *  该文件的代码来源于：http://yoyo08.javaeye.com/blog/309622
 * 非常感谢 yoyo08 同学共享贡献的代码。
 */
public class PDFOperate {
	/**
	 * 合并PDF文件
	 * 
	 * @param files
	 * @param savepath
	 */
	public static void mergePdfFiles(String[] files, String savepath) {
		try {
			Document document = new Document(new PdfReader(files[0])
					.getPageSize(1));
			PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));

			document.open();

			for (int i = 0; i < files.length; i++) {
				PdfReader reader = new PdfReader(files[i]);

				int n = reader.getNumberOfPages();

				for (int j = 1; j <= n; j++) {
					document.newPage();
					PdfImportedPage page = copy.getImportedPage(reader, j);
					copy.addPage(page);
				}
			}

			document.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	

	/**
	 * 切割PDF文件
	 * 
	 * @param filepath:
	 *            待切割文件路径 numbers: 切割文件的数目
	 */
	public static void partitionPdfFile(String filepath, int numbers) {
		Document document = null;
		PdfCopy copy = null;
		try {
			PdfReader reader = new PdfReader(filepath);

			// 获取行数
			int n = reader.getNumberOfPages();
			int N = numbers;

			if (n < N) {
				System.out.println("The document does not have " + N + " pages to partition !");
				return;
			}

			int size = n / N;
			String staticpath = filepath.substring(0, filepath.lastIndexOf("\\") + 1);
			String savepath = null;
			ArrayList<String> savepaths = new ArrayList<String>();
			for (int i = 1; i <= N; i++) {
				if (i < 10) {
					savepath = filepath.substring(filepath.lastIndexOf("\\") + 1, filepath.length() - 4);
					savepath = staticpath + savepath + "0" + i + ".pdf";
					savepaths.add(savepath);
				} else {
					savepath = filepath.substring(filepath.lastIndexOf("\\") + 1, filepath.length() - 4);
					savepath = staticpath + savepath + i + ".pdf";
					savepaths.add(savepath);
				}
			}

			for (int i = 0; i < N - 1; i++) {
				document = new Document(reader.getPageSize(1));
				copy = new PdfCopy(document, new FileOutputStream(savepaths.get(i)));
				document.open();
				for (int j = size * i + 1; j <= size * (i + 1); j++) {
					document.newPage();
					PdfImportedPage page = copy.getImportedPage(reader, j);
					copy.addPage(page);
				}
				document.close();
			}

			document = new Document(reader.getPageSize(1));
			copy = new PdfCopy(document, new FileOutputStream(savepaths.get(N - 1)));
			document.open();
			for (int j = size * (N - 1) + 1; j <= n; j++) {
				document.newPage();
				PdfImportedPage page = copy.getImportedPage(reader, j);
				copy.addPage(page);
			}
			document.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * <p>分隔文件
	*@author aaron.lu(luzhiyuan)
	*@param path
	*@param tofrom
	*@param toend
	*@throws Exception
	 */
	public static void partitionPdfFile(String path, int tofrom, int toend) throws Exception{
		Document document = null;
		PdfCopy copy = null;
		PdfReader reader = new PdfReader(path);

		// 获取行数
		int n = reader.getNumberOfPages();
		
		if(0 < tofrom && tofrom <= n && 0 < toend && toend <= n && tofrom < toend){
			
			document = new Document(reader.getPageSize(1));
			copy = new PdfCopy(document, new FileOutputStream(getPath(path, tofrom, toend)));
			document.open();
			for (int i = tofrom; i <= toend; i++) {
				document.newPage();
				PdfImportedPage page = copy.getImportedPage(reader, i);
				copy.addPage(page);
			}
			document.close();
		}else{
			throw new Exception("Invalid tofrom: "+tofrom+"or toend: "+toend+"");
		}
	}
	
	/**
	 * <p>获取页面数
	*@author aaron.lu(luzhiyuan)
	*@param path
	*@return
	*@throws IOException
	 */
	public static int getPdfPageCount(String path) throws IOException{
		PdfReader reader = new PdfReader(path);
		// 获取行数
		int n = reader.getNumberOfPages();
		return n;
	}
		
	private static String getPath(String path, int tofrom, int toend){
		String staticpath = path.substring(0, path.lastIndexOf("\\") + 1);
		String savepath = path.substring(path.lastIndexOf("\\") + 1, path.length() - 4);
		String followthrough = "";
		String resultpath = "";
		for (int i = tofrom; i <= toend; i++) {
			followthrough = followthrough + "_" + i;
		}
		resultpath = staticpath + savepath + resultpath + followthrough + Constant.POSTFIX_PDF_LOWCASE;
		return resultpath;
	}
	
	public static void main(String[] args) throws Exception {
		PDFOperate.partitionPdfFile("C:\\adss\\GoSign-DevelopersGuide.pdf", 2, 3);
		//PDFOperate.partitionPdfFile("C:\\adss\\GoSign-DevelopersGuide.pdf", 3);
	}
}

